package net.loodies;

import net.loodies.LoodiesAd;

import net.loodies.Manager;

import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import java.util.ArrayList;
public class LoodiesWrapper {

	

	// <!-- Android Ad Loodies/Swelen Slots ids -->
	public static Integer numLoodiesAdCount = new Integer(0);
	public static String menu_radio_ad_slotd_id		="fixed-320x50-test";
	public static String menu_podcast_ad_slotd_id	="fixed-320x50-test";
	public static String menu_titles_ad_slotd_id	="fixed-320x50-test";
	public static String splashscreen_ad_slotd_id	="splashscreen-test";
	public static String home_ad_slotd_id			="travelling-200x200-test";
	public static String radioplayer_ad_slotd_id	="travelling-200x200-test";
	public static String podcastplayer_ad_slotd_id	="travelling-200x200-test";
	public static String news_ad_slotd_id			="fixed-320x50-test";
	public static String horoscope_ad_slotd_id		="travelling-200x200-test";
	public static String weather_ad_slotd_id		="travelling-200x200-test";
        
	private static final String TAG = "LoodiesWrapper";
	
	private static boolean DO_USE_LOODIES = true;
	
	
	private static String mDeviceUid = "1234";
	
	private static LoodiesAd mLastAd= null; 
	private static ArrayList<LoodiesAd>  mLoodiesAdsArray = new ArrayList<LoodiesAd>();
	private static ArrayList<View> 		 mLoodiesViewsArray = new ArrayList<View>();
	private static Activity mActivityForNewsView;
	public static boolean mBringingToTheFront = false; 
	
	public static void setGravity(float travellerGravity){
		Manager.mTravellerGravity = travellerGravity;
	}
	public static void setDuration(long timeToDisapearMs){
		Manager.mTimeToDisapear = timeToDisapearMs;
	}

	public static void setParameters(float travellerGravity,float bouncerSpeedPixMs,long timeToDisapearMs){
		Manager.mTravellerGravity = travellerGravity;
		Manager.mBouncerSpeed   = bouncerSpeedPixMs;
		Manager.mTimeToDisapear = timeToDisapearMs;
	}
	
	public static void RadiomeeAddAdHome(Activity activity){
		
		if(DO_USE_LOODIES){
			net.loodies.LoodiesWrapper.RemoveLastAd();
			net.loodies.LoodiesWrapper.setGravity(4.8f);
			net.loodies.LoodiesWrapper.setDuration(8000);
			net.loodies.LoodiesWrapper.AddAd(activity, net.loodies.LoodiesWrapper.home_ad_slotd_id);
		}	
	}
	public static void RadiomeeAddAdRadioPlayer(Activity activity){
		if(DO_USE_LOODIES){
			net.loodies.LoodiesWrapper.RemoveLastAd();
			net.loodies.LoodiesWrapper.setGravity(4.8f);
			net.loodies.LoodiesWrapper.setDuration(8000);
			net.loodies.LoodiesWrapper.AddAd(activity, net.loodies.LoodiesWrapper.radioplayer_ad_slotd_id);
		}
	}

	public static void RadiomeeAddAdPodcastPlayer(Activity activity){
		if(DO_USE_LOODIES){
			net.loodies.LoodiesWrapper.RemoveLastAd();
			net.loodies.LoodiesWrapper.setGravity(4.8f);
			net.loodies.LoodiesWrapper.setDuration(8000);
			net.loodies.LoodiesWrapper.AddAd(activity, net.loodies.LoodiesWrapper.podcastplayer_ad_slotd_id);
		}
	}

	public static String getUserID(Context context){
		TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		String uid =  tm.getDeviceId();
		// Get rid of space 
		uid = uid.replace(" ", "%20");
		return uid;
	}

	public static void RemoveAd(LoodiesAd loodiesad) {
		if(loodiesad!=null){
			if(loodiesad.getParentActivity() != null){
				// Get Main view
				View v = loodiesad.getParentActivity().getWindow().getDecorView();

				if(v==null){
					return;
				}
				// Get Main view as ViewGroup
				ViewGroup viewgroup = (ViewGroup)v;

				if(viewgroup==null){
					return;
				}
				// Remove First child from hierarchy
				viewgroup.removeView(loodiesad);

				// Retrieve old First View
				View childZero = loodiesad.getChildAt(0);

				// Detach it from Ad Layout o
				loodiesad.removeView(childZero);

				// Replace the first child of view
				viewgroup.addView(childZero);

				// Shutdown ad view
				loodiesad.Shutdown();
				
				numLoodiesAdCount--;
		
				mLoodiesAdsArray.remove(loodiesad);
					
				Log.d(TAG, "loodiesad.Shutdown() numLoodiesAdCount:" + numLoodiesAdCount.toString());
				
				
			}
		}
	}

	public static void RemoveLastAd() {
		RemoveAd(mLastAd);
		mLastAd = null;
	}
	
	public static void RemoveAllAds() {
		for (int i = 0 ;i<mLoodiesAdsArray.size();i++){
			LoodiesAd ad = mLoodiesAdsArray.get(i);
			RemoveAd(ad);
			ad = null;
		}
		mLoodiesAdsArray.removeAll(mLoodiesAdsArray);
		
		for (int i = 0 ;i<mLoodiesViewsArray.size();i++){
			View ad = mLoodiesViewsArray.get(i);
			ad = null;
		}
		mLoodiesViewsArray.removeAll(mLoodiesViewsArray);
		mLastAd = null;
	}
	
	public static LoodiesAd AddAd(Callback cb,Activity activity,String slotID) {
		LoodiesAd ad = AddAd(activity,slotID);
		ad.mCallback = cb;
		return ad;
	}

	public static LoodiesAd AddAd(Activity activity,String slotID) {

		if(slotID==null){
			Log.v(TAG, "slotID must be not null");
			return null;
		}
		
		// Check sanity
		Check(activity);

	
		Log.d(TAG, "new LoodiesAd(activity) numLoodiesAdCount:" + numLoodiesAdCount.toString());
		
		// Get Main view
		View v = activity.getWindow().getDecorView();

		// Get Main view as ViewGroup
		ViewGroup viewgroup = (ViewGroup)v;

		// assert viewgroup.children > 0
		View firstchild = viewgroup.getChildAt(0);

		// Remove First child from hierarchy
		viewgroup.removeView(firstchild);

		// Instantiate new LoodiesAd
		LoodiesAd loodiesAd = new LoodiesAd(activity);
		mLoodiesAdsArray.add(loodiesAd);
		numLoodiesAdCount++;	
		
		// Add lonesome child to LoodiesAd Layout
		loodiesAd.addView(firstchild);

		// Add LoodiesAd to Main View
		viewgroup.addView(loodiesAd);

		loodiesAd.LoadAndStart(slotID, mDeviceUid,activity);

		mLastAd = loodiesAd;
		return loodiesAd;
	}
	@SuppressWarnings("unused")
	public static LoodiesFixedView AddAdAsView(Activity activity,String slotID) {

		if(slotID==null){
			Log.v(TAG, "slotID must be not null");
			return null;
		}
		
		// Check sanity
		Check(activity);
		// Instantiate new LoodiesView
		LoodiesFixedView fv = new LoodiesFixedView(activity,LoodiesWrapper.menu_radio_ad_slotd_id,mDeviceUid);
		if(fv == null){
			Log.v(TAG, "LoodiesFixedView creation failed");
			return null;	
		}

		mLoodiesViewsArray.add(fv);
		numLoodiesAdCount++;	
		Log.d(TAG, "new LoodiesFixedView(activity) numLoodiesAdCount:" + numLoodiesAdCount.toString());
		return fv;
	
		
	}
	public static LoodiesAd AddAd(View view,Activity activity,String slotID) {

		if(slotID==null){
			Log.v(TAG, "slotID must be not null");
			return null;
		}
		
		// Check sanity
		Check(activity);

		// Instantiate new LoodiesAd
		LoodiesAd loodiesAd = new LoodiesAd(activity);
		mLoodiesAdsArray.add(loodiesAd);
		numLoodiesAdCount++;	
		
		Log.d(TAG, "new LoodiesAd(activity) numLoodiesAdCount:" + numLoodiesAdCount.toString());
		
		// Get Main view
		View v = view;

		// Get Main view as ViewGroup
		ViewGroup viewgroup = (ViewGroup)v;

		// assert viewgroup.children > 0
		View firstchild = viewgroup.getChildAt(0);

		// if there is not a child, then add ad to view directly
		if(firstchild==null)
		{
			
		}
		else
		{
			// Remove First child from hierarchy
			viewgroup.removeView(firstchild);
			
			// Add lonesome child to LoodiesAd Layout
			loodiesAd.addView(firstchild);

		}
		
		// Add LoodiesAd to Main View
		viewgroup.addView(loodiesAd);

		loodiesAd.LoadAndStart(slotID, mDeviceUid,activity);

		mLastAd = loodiesAd;
		return loodiesAd;
	}
	public static void Check(Activity activity){
		if(mDeviceUid==null){
			mDeviceUid = getUserID(activity);
		}
	}
	
	public static void RadiomeeArticleNoAdds(){
		if(DO_USE_LOODIES){
			RemoveAllAds();
		}
	}
	
	/**
	 *  this needs that an activity has been set up.
	 */
	public static void RadiomeeAddAdIntoNewsView(){
		if(DO_USE_LOODIES){
			
			RemoveAllAds();
			if(mActivityForNewsView == null){
				Log.e(TAG, "mActivityForNewsView is null");	
				return;
			}
			AddAd(mActivityForNewsView,news_ad_slotd_id);
		}
	}
	public static void RadiomeeMenuRadio(Activity activity){
		if(DO_USE_LOODIES){
			AddAd(activity, menu_radio_ad_slotd_id);
		}
	}
	public static void RadiomeeMenuPodcast(Activity activity){
		if(DO_USE_LOODIES){
			AddAd(activity, menu_podcast_ad_slotd_id);
		}
	}
	public static void RadiomeeMenuTitles(Activity activity){
		if(DO_USE_LOODIES){
			AddAd(activity, menu_titles_ad_slotd_id);
		}
	}
	public static void RadiomeeSetActivityForNewsView(Activity activity){
		
		if(DO_USE_LOODIES){
			mActivityForNewsView = activity;
		}
	}
	public static void RadiomeeRemoveAllAds() {
		if(DO_USE_LOODIES){
			RemoveAllAds();
		}
	}
	public static void RadiomeeBrowsingNewsGetBack() {
		if(DO_USE_LOODIES){
			RemoveAllAds();
			if(mActivityForNewsView == null){
				Log.e(TAG, "mActivityForNewsView is null");	
				return;
			}
			AddAd(mActivityForNewsView,news_ad_slotd_id);
		}
	}
	public static void RadiomeeArticleKeyBack() {
		if(DO_USE_LOODIES){
			RemoveAllAds();
			if(mActivityForNewsView == null){
				Log.e(TAG, "mActivityForNewsView is null");	
				return;
			}
			AddAd(mActivityForNewsView,news_ad_slotd_id);
		}
		
	}
	public static LoodiesAd RadiomeeAddSplashScreenAd(Callback cb,Activity activity) {
		if(DO_USE_LOODIES){
			return net.loodies.LoodiesWrapper.AddAd(cb,activity, splashscreen_ad_slotd_id);
		}
		return null;
	}
}
